package com.dao;

import com.model.Goods;
import com.tools.ChStr;
import com.tools.ConnDB;

public class GoodsDaoImpl implements GoodsDao {
	ConnDB conn = new ConnDB();//加载tools包下的方法类
	ChStr chStr = new ChStr();//加载tools包下的方法类
	
	/**
	 * 新增操作
	 * 
	 */
	public int insert(Goods g) {
		int ret = -1;//设定返回值变量
		try {
			//设定执行语句
			String sql = "Insert into goods (id,name,introduction,original_price,current_price,picture,is_new,is_sale) values("
					+ g.getTypeID()
					+ ",'"
					+ chStr.chStr(g.getGoodsName())
					+ "','"
					+ chStr.chStr(g.getIntroduce())
					+ "',"
					+ g.getPrice()
					+ ","
					+ g.getPrice()
					+ ",'"
					+ chStr.chStr(g.getPicture())
					+ "',"
					+ g.getNewGoods()
					+ "," + g.getSale() + ")";

			ret = conn.executeUpdate(sql);//执行语句，与数据库相连接
		} catch (Exception e) {
			ret = 0;
		}
		conn.close();//关闭数据库连接
		return ret;
	}

	/**
	 * 更新操作
	 * 
	 */
	public int update(Goods g) {
		int ret = -1;//设定返回值变量
		try {
			//设定执行语句
			String sql = "update goods set subcat_id=" + g.getID()
					+ ",name='" + chStr.chStr(g.getGoodsName())
					+ "',introduction='" + chStr.chStr(g.getIntroduce())
					+ "',original_price=" + g.getPrice() + ",current_price="
					+ g.getNowPrice() + ",picture='"
					+ chStr.chStr(g.getPicture()) + "',is_new="
					+ g.getNewGoods() + ",is_sale=" + g.getSale() + " where id="
					+ g.getID();
			ret = conn.executeUpdate(sql);//执行语句，与数据库相连接
		} catch (Exception e) {
			ret = 0;
		}
		conn.close();//关闭数据库连接
		return ret;
	}
	
	/**
	 * 删除操作
	 * 
	 */
	public int delete(Goods g) {
		int ret = -1;//设定返回值变量
		try {
			//设定执行语句
			String sql = "Delete from goods where id=" + g.getID();
			ret = conn.executeUpdate(sql);//执行语句，与数据库相连接
		} catch (Exception e) {
			ret = 0;
		}
		conn.close();//关闭数据库连接
		return ret;

	}

}